/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import controller.CustomerJpaController;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.entity.Customer;

/**
 *
 * @author lnngu_000
 */
@ManagedBean
public class CustomerBean extends ActionSupport {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("OnlineCateringPU");
    private String fullname;
    private int customerId;

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
    private String address;
    private String email;
    private String password;
    private String phone;
    private String birthday;
    private String gender;
    private Boolean isActive;
    private String descriptions;

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    public String getDescriptions() {
        return descriptions;
    }

    public void setDescriptions(String descriptions) {
        this.descriptions = descriptions;
    }
    CustomerJpaController customerControl = new CustomerJpaController(emf);

    public String add() {
        try {
            
             Customer customer = new Customer();
             customer.setDescriptions(this.getDescriptions());
             customer.setIsActive(this.getIsActive());
             customer.setBirthday(this.getBirthday());
             customer.setPassword(this.getPassword());
             customer.setAddress(this.getAddress());
             customer.setEmail(this.getEmail());
             customer.setFullname(this.getFullname());
             customer.setGender(this.getGender());
             customer.setPhone(this.getPhone());
             customer.setIsActive(true);
             customerControl.create(customer);
             addActionMessage("The registration successfull. ");
            return "success";
        } catch (Exception e) {
            addActionError("The registration fail. ");
            return "fail";
        }

    }

    public String edit() {
        try {

            Customer customer = new Customer();
            customer.setCustomerId(this.getCustomerId());
            customer.setDescriptions(this.getDescriptions());
            customer.setIsActive(this.getIsActive());
            customer.setBirthday(this.getBirthday());
            customer.setPassword(this.getPassword());
            customer.setAddress(this.getAddress());
            customer.setEmail(this.getEmail());
            customer.setFullname(this.getFullname());
            customer.setGender(this.getGender());
            customer.setPhone(this.getPhone());
            customerControl.edit(customer);        
            return "success";
        } catch (Exception e) {
        }        
        return "fail";
    }

    public String findCustomer() {
        try {
            Map session = ActionContext.getContext().getSession();
            session.put("Customer", customerControl.findCustomer(this.getCustomerId()));
            return "success";
        } catch (Exception e) {
        }
        return "fail";
    }

    @Override
    public void validate() {
        if (this.getFullname().equals("")) {
            addActionError("Fullname is required.");
        }
        if (this.getPassword().equals("")) {
            addActionError("Password is required.");
        }
        if (this.getEmail().equals("")) {
            addActionError("Email is required.");
        } else {
            String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
            Pattern p = Pattern.compile(regex);
            Matcher matcher = p.matcher(this.getEmail().trim());
            if (!matcher.find()) {
                addActionError("Email address not validated.");
            }
        }
        if (this.getPhone().equals("")) {
            addActionError("Phone is required.");
        } 
        if (this.getBirthday().equals("")) {
            addActionError("Birthday is required.");
        } 
    }
}
